package com.imchen.cms.cmsweb.controller;

import com.imchen.cms.annotation.UserLoginToken;
import com.imchen.cms.exception.CmsException;
import com.imchen.cms.model.Article;
import com.imchen.cms.model.Html;
import com.imchen.cms.service.ArticleService;
import com.imchen.cms.utils.ServiceUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.util.HashMap;

/**
 * @author imchen
 * @create: 2019-01-11 16:23
 **/
@RestController
@RequestMapping(value = "/article")
public class ArticleController implements BaseController<Article>{

    @Autowired
    private ArticleService articleService;

    @Override
    @UserLoginToken
    @GetMapping(value = "/find/{id}")
    public HashMap<String, Object> find(@PathVariable int id){
        return ServiceUtil.toObjectMap(articleService.find(id),"article");
    }

    @GetMapping("/find/articles")
    public HashMap<String, Object> find(@RequestParam int index, @RequestParam int pageSize){
        return ServiceUtil.toObjectMap(articleService.findByPage(index,pageSize),"article");
    }

    @GetMapping("/find/pages")
    public HashMap<String, Object> findByPage(@RequestParam int index, @RequestParam int pageSize,@RequestParam(required = false) String tags){
        return ServiceUtil.toObjectMap(articleService.findByPageHelper(index,pageSize,tags),"article");
    }

    @Override
    @PostMapping(value = "/add")
    @UserLoginToken
    public HashMap<String, Integer> add(@RequestBody Article article) throws Exception {
        return articleService.add(article);
    }

    @PostMapping(value = "/deploy")
    @UserLoginToken
    public HashMap<String, Integer> deploy(@RequestBody Article article) throws Exception {
        Html html =articleService.deploy(article,null);
        if (html!=null){
            return ServiceUtil.toAddMap(1,html.getId());
        }
        return ServiceUtil.toAddMap(0,0);
    }

    @UserLoginToken
    @PostMapping("/add/deploy")
    public HashMap<String,Integer> addAndDeploy(@RequestBody Article article) throws Exception {
        return articleService.addAndDeploy(article);
    }

    @PostMapping("/redeploy")
    @UserLoginToken
    public HashMap<String,Integer> reDeploy(@RequestBody String json){
        JSONObject jsonObject=new JSONObject(json);
        int articleId =jsonObject.getInt("articleId");
        int templateId = jsonObject.getInt("templateId");
        boolean result=articleService.reDeploy(articleId,templateId);
        if (result){
            return ServiceUtil.toAddMap(1,articleId);
        }
        return ServiceUtil.toAddMap(0,0);
    }

    @Override
    @PostMapping(value = "/update")
    @UserLoginToken
    public int update(@RequestBody Article article){
        return articleService.update(article);
    }

    @Override
    @GetMapping(value = "/remove")
    @UserLoginToken
    public int remove(@PathVariable int id){
        return articleService.remove(id);
    }

    @Override
    @DeleteMapping("/delete/{id}")
    @UserLoginToken
    public int delete(@PathVariable int id) {
        return articleService.deleteById(id);
    }

    @DeleteMapping("/restore/{id}")
    @UserLoginToken
    public int restore(@PathVariable int id) {
        return articleService.restoreById(id);
    }

    @GetMapping(value = "/{id}/{fileName}",produces = MediaType.TEXT_HTML_VALUE)
    public ResponseEntity<Resource> view(@PathVariable int id,@PathVariable String fileName, HttpServletResponse response) throws CmsException, FileNotFoundException {
        return articleService.view(id,fileName);
    }
}